package com.fs.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.base.pojo.Menu;
import com.fs.base.pojo.MenuMotion;
import com.fs.base.pojo.Motion;
import com.fs.base.repository.MenuMotionRepository;
import com.fs.base.repository.MotionRepository;
import com.fs.base.service.MenuService;
import com.fs.base.vo.DataGrid;
import com.fs.base.vo.JsonResponse;
import com.fs.base.vo.SysPath;

/**
 * @author TiddlerCJ
 * @desc 菜单管理业务处理器
 */
@Controller
@RequestMapping(SysPath.BACK_MENU_PATH)
public class MenuController {
	private static final Logger log = Logger.getLogger(MenuController.class);
	@Autowired private MenuMotionRepository menuMotionRepository;
	@Autowired private MenuService menuService;
	@Autowired private MotionRepository motionRepository;
	
	@ResponseBody
	@RequestMapping("/menuMotionConfig")
	public JsonResponse menuMotionConfig(String menuId, HttpServletRequest request) {
		try {
			List<MenuMotion> list = new ArrayList<MenuMotion>();
			String motionJsonArray = request.getParameter("motionJsonArray");
			JSONArray array = JSONArray.parseArray(motionJsonArray);
			for(int i=0,len=array.size();i<len;i++){
				Motion motion = JSON.toJavaObject(array.getJSONObject(i), Motion.class);
				MenuMotion menuMotion = new MenuMotion(menuId, motion);
				list.add(menuMotion);
			}
			menuService.configMotion(menuId, list);
			return JsonResponse.success();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return JsonResponse.error(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/menuMotionList")
	public DataGrid motionList(String menuId){
		DataGrid grid = new DataGrid();
		try {
			List<Motion> gridList = new ArrayList<Motion>();
			/*
			 * 系统所有动作列表
			 */ 
			List<Motion> motionList = motionRepository.findAll();
			/*
			 * 预选配置动作列表 
			 */
			List<MenuMotion> menuMotionList = menuMotionRepository.findByMenuId(menuId);
			Map<String, MenuMotion> map = new HashMap<String, MenuMotion>();
			for(MenuMotion mm:menuMotionList){
				map.put(mm.getMotionId(), mm);
			}
			for(Motion motion:motionList){
				if(map.containsKey(motion.getId())){
					MenuMotion menuMotion = map.get(motion.getId());
					motion.setMotionName(menuMotion.getMotionName());
					motion.setMotionOrder(menuMotion.getMotionOrder());
					motion.setIsChecked(true);
				}
				gridList.add(motion);
			}
			grid = DataGrid.gridBuilder(gridList);
		} catch (Exception e) {
			grid = DataGrid.gridBuilder(new ArrayList<JSONObject>());
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return grid;
	}
	
	/**
	 * 菜单移动
	 * @param req
	 * @return
	 */
	@GetMapping("move")
	public String move(HttpServletRequest req) {
		return SysPath.BACK_MENU_PATH.concat("/move");
	}
	
	@ResponseBody
	@RequestMapping("/move")
	public JsonResponse moveConfig(String id,String parentId, HttpServletRequest request) {
		try {
			if(StringUtils.isNotBlank(parentId)){
				Menu menu = menuService.getMenuRepository().findOne(id);
				if(menu!=null){
					menu.setParentId(parentId);
					menuService.getMenuRepository().save(menu);
				}
				return JsonResponse.success();
			}else{
				return JsonResponse.error("未选定父菜单！");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return JsonResponse.error(e.getMessage());
		}
	}
}
